/**
 * 单元格 渲染组件
 * */
import React from "react";
import {ICommonTableAttributeColumnOption} from "@/biz-components/claim-items/components-v2/CommonTable";
import {justRequired} from "./common-validator";
import {ValidateStyleAdjustment} from "@/biz-components/claim-items/components-v2/cell-input";

export interface ICellProps {
	// 字段属性名称
	name: string;
	// 这一行信息
	recordData: any;
	// param[0] 就是props传入的对象。不会传一个新的对象回去 (因为数据没有id，父组件不能判断修改的是哪条数据)
	onChange: (recordData: any) => void;
	// 是否需要显示校验信息
	shouldShowValidateMessage: boolean;
	// 向父组件添加或删除自己的校验方法
	addValidator: (validator: () => boolean) => void;
	removeValidator: (validator: () => boolean) => void;

	// 表格这一列的配置
	columnOption: ICommonTableAttributeColumnOption<any, any> | null;
	// 元模型配置
	meta: any;
	detailMode: boolean;

}
export class CommonTableCell extends React.Component<ICellProps> {
	constructor(props: ICellProps) {
		super(props);
		this.validate = this.validate.bind(this);
	}
	componentDidMount() {
		this.props.addValidator(this.validate);
	}
	componentWillUnmount() {
		this.props.removeValidator(this.validate);
	}

	/**
	 * 校验这一个单元格数据
	 * 校验通过返回true 否则返回false
	 * */
	validate(): boolean {
		const {name, recordData = {}, meta = {}, columnOption} = this.props;

		if(this.checkIfDisabled()) return true;

		const value = recordData[name];
		let invalid = false;

		// 元模型配置了必填
		const metaConfigRequired = meta.props && (meta.props.required === '1');
		if(metaConfigRequired) {
			invalid = !justRequired(value);
		}

		// 表格列配置了校验规则
		if(columnOption && columnOption.validator) {
			invalid = !columnOption.validator(value, recordData);
		}


		return !invalid;
	}
	checkIfDisabled() {
		const {recordData = {},columnOption} = this.props;
		// 判断是否禁用
		let disabled = false;
		if(!columnOption) {
			disabled = false;
		} else if(typeof columnOption.disabled === 'boolean') {
			disabled = columnOption.disabled;
		} else if(typeof columnOption.disabled === 'function') {
			disabled = columnOption.disabled(recordData);
		}
		return disabled;
	}

	render() {
		const {recordData = {}, name, detailMode, columnOption, shouldShowValidateMessage, meta} = this.props;
		let value = "";
		try {
			value = recordData[name];
		} catch (e) {
			console.error(e);
		}

		let showName = false;
		let valueForShowName = '';
		if(meta.props && meta.props.columnRenderType === 'DICT_TYPE') {
			if(recordData[name + 'Name']) {
				showName = true;
				valueForShowName = recordData[name + 'Name'];
			}

		}
		if(detailMode) {
			return (columnOption && columnOption.renderDetail) ?
				columnOption.renderDetail(value, recordData)
				:
				<span>{showName ? valueForShowName : value}</span>
		}

		if(!columnOption) {
			return <span>{showName ? valueForShowName : value}</span>
		}
		if(!columnOption.component) {
			return <span>{showName ? valueForShowName : value}</span>
		}

		let disabled = this.checkIfDisabled();

		let [Comp, extraProps = {}] = columnOption.component;
		if(typeof extraProps === 'function') {
			extraProps = extraProps(recordData);
		}
		const invalid = shouldShowValidateMessage && !this.validate();


		return (
			<ValidateStyleAdjustment invalid={invalid}>
				<Comp
					value={value}
					onChange={(v: any, n: any) => {
						recordData[name] = v;
						// 名称的联动处理
						if (columnOption?.relationForNameAttribute) {
							if(n !== undefined) {
								recordData[columnOption.relationForNameAttribute] = n;
							}
						}
						// 一行数据的变化之后的其他处理
						if (columnOption?.onRowChange) {
							columnOption.onRowChange(recordData);
						}

						this.props.onChange(recordData);
					}}
					disabled={disabled}
					{...extraProps}
				/>
			</ValidateStyleAdjustment>
		)
	}
}
